package com.xyauto.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.widget.RemoteViews;

import com.xyauto.Dialog.MyFloatDialog;
import com.xyauto.xfq.IMyAidlInterface;
import com.xyauto.xfq.R;

import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;


public class AuxiliaryService extends Service
{
    private  final String TAG = "AuxiliaryService";
	private Intent mIntent;
	private int device_break  = -1;
	private boolean  isServiceSrart  = false;
	private static 	MyFloatDialog mBaseFloatDailog;

	private Notification notification;//通知
	private String notificationChannelId="Dialog";//通知渠道id
	private static final int notifyId=10;
	NotificationCompat.Builder mBuilder;//notification建造者

	private MyBinder myBinder;

	static class MyBinder extends IMyAidlInterface.Stub{

		@Override
		public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
							   double aDouble, String aString) throws RemoteException {

		}
	}

	@Override
	public IBinder onBind(Intent intent)
	{
		return myBinder;
	}

	public void onCreate()
	{
		myBinder = new MyBinder();
		// 创建service时一个 实例化一个TableShowView对象并且调用他的fun()方法把它注册到windowManager上
		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		bindService(new Intent(this, RemoteService.class), new MyServiceConnection(),
				BIND_AUTO_CREATE);
		mIntent = intent;
		if (mBaseFloatDailog==null){
		mBaseFloatDailog = new MyFloatDialog(this.getApplication());
		mBaseFloatDailog.show();}else {
			mBaseFloatDailog.dismiss();
			mBaseFloatDailog = new MyFloatDialog(this.getApplication());
			mBaseFloatDailog.show();
		}

		initNotificationBar();

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			startForeground(notifyId,notification);
		} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
			//如果 18 以上的设备 启动一个Service startForeground给相同的id
			//然后结束那个Service
			startForeground(notifyId, new Notification());
			startService(new Intent(this, InnnerService.class));
		} else {
			startForeground(notifyId, new Notification());
		}
		return super.onStartCommand(intent, flags, startId);

	}



	//初始化Notification通知
	private void initNotificationBar(){
		//检查当前Android版本，8.0以上需设置通知渠道
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			CharSequence name = "通知栏播放";
			String description = "notification description";
			int importance = NotificationManager.IMPORTANCE_MIN;
			NotificationChannel mChannel = new NotificationChannel(notificationChannelId, name, importance);
			mChannel.setDescription(description);
			mChannel.setLightColor(Color.RED);
			mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
			mNotificationManager.createNotificationChannel(mChannel);
		}
		mBuilder=new NotificationCompat.Builder(this,notificationChannelId);


		//设置通知栏样式
		mBuilder.setWhen(System.currentTimeMillis())
				.setContentTitle("悬浮窗~")
				.setSmallIcon(R.drawable.music_icon)
				.setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级
				.setOngoing(false)//ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
				.setDefaults(Notification.DEFAULT_ALL);//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合：
				/*.setContent(remoteView)
				.setSmallIcon(R.drawable.icon_music)
				.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.icon_music))*/;
		notification=mBuilder.build();
		notification.flags = Notification.FLAG_ONGOING_EVENT;//设置通知点击或滑动时不被清除
		NotificationManager manager=(NotificationManager)getSystemService(Service.NOTIFICATION_SERVICE);
		manager.notify(notifyId,notification);

		/////---------------
	}

	private class MyServiceConnection implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
			//回调

		}

		@Override
		public void onServiceDisconnected(ComponentName componentName) {
			//
			startService(new Intent(AuxiliaryService.this, RemoteService.class));
			bindService(new Intent(AuxiliaryService.this, RemoteService.class), new MyServiceConnection(),
					BIND_AUTO_CREATE);
		}
	}

	public static class InnnerService extends Service {

		@Override
		public void onCreate() {
			super.onCreate();
			startForeground(notifyId, new Notification());
			stopSelf();
		}

		@Nullable
		@Override
		public IBinder onBind(Intent intent) {
			return null;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		//stopForeground(true);// 停止前台服务--参数：表示是否移除之前的通知
	}
}